﻿using Adnc.Application.Shared;
using Adnc.Application.Shared.Dtos;
using Adnc.WebApi.Shared;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MoBaoPay.Model.Request;
using WYT.Pay.Application.Contracts.Dtos.Payment;
using WYT.Pay.Application.Contracts.Services;

namespace WYT.Pay.WebApi.Controllers
{
    /// <summary>
    /// 支付
    /// </summary>
    [Route("pay/[Controller]")]
    [ApiController]
    public class PaymentController : AdncControllerBase
    {
        private readonly IMoBaoAppService _mobaoAppService;

        private readonly IUserContext _userContext;

        private readonly IPaymentAppService _paymentAppService;

        public PaymentController(IMoBaoAppService mobaoAppService
            , IUserContext userContext
            , IPaymentAppService paymentAppService)
        {
            _mobaoAppService = mobaoAppService;
            _userContext = userContext;
            _paymentAppService = paymentAppService;
        }


        /// <summary>
        /// 小程序支付
        /// </summary>
        /// <returns></returns>
        [HttpPost("wxpay")]
        public async Task<WYTActionResult<WechatPayResponse>> WXOpenPayment(SimpleInputDto<long> input)
        {
            return Success(await _mobaoAppService.GetWXPaymentInfor(_userContext.Id, input));
        }

        /// <summary>
        /// 支付回调
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("notify")]
        [AllowAnonymous]
        public async Task<bool> NotifyVerify(MoBaoNotifyInput input)
        {
            var result = await _mobaoAppService.NotifyVerify(input);
            return result.IsSuccess;
        }

         
        #region 内部接口
        /// <summary>
        /// 创建支付单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("_orderPay")]
        public async Task<ActionResult<long>> OrderPayCreate(OrderPayCreateDto input)
        {
            return Result(await _paymentAppService.CreateOrderPay(_userContext.Id, input));
        }
        #endregion
    }
}
